package com.wnx.db;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.wnx.entity.Goods;
import com.wnx.entity.Users;
import com.wnx.util.HibernateUtil;

public class GoodsDao {
	
public GoodsDao(){
		
	}
	public String saveG(Goods goods){
		 String message = "error";
		 Session session = new HibernateUtil().getSession();
		 Transaction transaction = session.beginTransaction();
		 session.save(goods);
		 transaction.commit();
		 session.close();
		 message = "success";
		 return message;
	 }
	public List getGoodsByUid(Goods good){
		Session session = new HibernateUtil().getSession();
		Transaction transaction = session.beginTransaction();
		String hql = "from Goods where uid=?";
		Query<Goods> query = session.createQuery(hql,Goods.class);
		query.setParameter(0, good.getUid());
		List list=query.list();
        transaction.commit();
        session.close();
		return list;
	}
	public Goods updGoodsState(Goods goods){
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		String hql ="update Goods set state=1 where id=?";
		Query query = session.createQuery(hql);
		query.setParameter(0, goods.getId());
		query.executeUpdate();
		transaction.commit();
        session.close();
		return goods;
		
	}
}
